package com.gohome.gohomechat.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.model.entify.Notice;
import com.gohome.gohomechat.presenter.NoticePresenter;
import com.gohome.gohomechat.presenter.presenterlistener.NoticePresenterInterface;
import com.gohome.gohomechat.ui.adapter.NoticeListViewAdapter;
import com.gohome.gohomechat.ui.viewlistener.NoticeViewInterface;
import com.gohome.gohomechat.utils.ToastUtil;

import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class NoticeActivity extends BaseActivity implements View.OnClickListener, NoticeViewInterface, NoticeListViewAdapter.onClickButtonListener {
    private ListView listView;
    private ImageButton notice_imgbtn_back;
    private NoticeListViewAdapter adapter;
    private NoticePresenterInterface noticePresenterInterface;
    private String[] actions = {Contant.ACTION_CONTACT_CHANAGED,
            Contant.ACTION_CONTACT_REFUSED, Contant.NEW_FRIENDS_INVITATION,Contant.NEW_FRIENDS_ADD,Contant.ACCOUNT_REMOVED};

    @Override
    protected int getLayout() {
        return R.layout.activity_notice;
    }

    @Override
    protected void initPresenter() {
        this.noticePresenterInterface = new NoticePresenter(this);
    }

    @Override
    protected void initView() {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        for (int i = 0; i < actions.length; i++) {
            IntentFilter intentFilter = new IntentFilter(actions[i]);
            localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
        }

        listView = findView(R.id.notice_listview);
        notice_imgbtn_back = findView(R.id.notice_imgbtn_back);
        adapter = new NoticeListViewAdapter();
    }

    @Override
    protected void initData() {
        listView.setAdapter(adapter);
        notice_imgbtn_back.setOnClickListener(this);
        noticePresenterInterface.refreshData();

        adapter.setOnClickButtonListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.notice_imgbtn_back:
                finish();
                break;
        }
    }

    @Override
    public void refreshData(List<Notice> datas) {
        adapter.addData(datas);
    }

    @Override
    public void refreshDataFailed() {
        ToastUtil.showToast("通知信息拉取失败!");
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            noticePresenterInterface.refreshData();
        }
    };

    @Override
    public void agreeRequest(String userName) {
        noticePresenterInterface.agreeFriendRequest(userName);
    }

    @Override
    public void refusedRequest(String userName) {
        noticePresenterInterface.refusedFriendRequest(userName);
    }
}
